
var dataSource="网络"
var socketOpen = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:"",
    content:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.onSocketOpen(function (res) {
      console.log("webSocket链接已打开!")
    })
    wx.onSocketError(function (res) {
      console.log("webSocket链接打开失败")
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 网络请求数据
  requestData:function(){
    console.log("网络请求")
    wx.request({
      url:"http://123.206.174.209:81/AppUpdate/AppLoad",
      data:{},
      header:{
        "appVersion": "ios_1.5.8",
        "application": "ios",
        "channelCode": "ios",
        "deviceNo": "743e567460a44f808aa7e9350c0c34b8",
        "userToken": "89D5D668 - 3B0E-425E-893E-CA2919D75887",
        "versionCode": 1200
      },
      success:function(res){
        console.log(res.data)
        dataSource = res.data.toString()
      }
    })
  },
  uploadImg:function(){
    var that = this
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        that.setData({
          src:tempFilePaths
        })
        const uploadTask = wx.uploadFile({
          url: 'http://example.weixin.qq.com/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData:{
            "user":"test"
          },
          success:function(res){
            console.log(res.data)
          },
          fail:function(res){
            console.log(res)
          },
        })

        uploadTask.onProgressUpdate((res) => {
          console.log('上传进度'+res.progress)
          console.log('已经上传的数据长度'+res.totalBytesSent)
          console.log('预期需要上传数据总长度' + res.totalBytesExpectedToSend)
        })
      },
    })
  },
  downloadImg:function(){
    var source = "http://p1509fp8v.bkt.clouddn.com/20180626184726851_447a7d7b3a07465688d76b91cfea893d.zip"
    const downTask = wx.downloadFile({
      url:source,
      success:function(res){
        console.log(res.tempFilePath)
      }
    })
    downTask.onProgressUpdate((res) => {
      console.log('下载进度', res.progress)
      console.log('已经下载的数据长度', res.totalBytesWritten)
      console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
    })
  },
  webSocket:function(){
    wx.connectSocket({
      url: 'ws://123.207.167.163:9010/ajaxchattest',
      protocols: ['protocol1'],
      method:'GET',
      success:function(res){
        socketOpen = true
        console.log(res)
      },
      fail:function(res){
        console.log(res)
      },
      complete:function(res){
        console.log(res)
      }
    })

  },
  sendMessage:function(){
    if(socketOpen){
      wx.sendSocketMessage({
        data: this.data.content,
      })

      this.setData({
        content:""
      })
    }else{
      console.log("链接未打开")
    }

    wx.onSocketMessage(function(res){
      console.log("服务器回复的内容:"+res.data)
    })
  },
  closeSocket:function(){
    wx.closeSocket({
      
    })

    wx.onSocketClose(function(res){
      socketOpen = false
      console.log("websocket已关闭")
    })
  },
  bindinput:function(e){
    console.log(e.detail.value)
    this.setData({
      content:e.detail.value
    })
  }
})